package nl.ou.bls.server;

import java.io.IOException;
import java.util.*;
import java.util.Date;
import java.util.logging.Logger;
import java.io.*;
import java.lang.String;
import java.util.Arrays;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class BLSDataService extends HttpServlet {
    private static final Logger log =
            Logger.getLogger(BLSDataService.class.getName());

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        // We have one entity group per Guestbook with all Greetings residing
        // in the same entity group as the Guestbook to which they belong.
        // This lets us run an ancestor query to retrieve all Greetings for a
        // given Guestbook. However, the write rate to each Guestbook should be
        // limited to ~1/second.
        String blsDataSetName = req.getParameter("email");
        Key blsDataSetKey = KeyFactory.createKey("BLSDataSet", blsDataSetName);
        String content = req.getParameter("content");
        Date date = new Date();
        Entity blsDataSet = new Entity("BLSDataSet", blsDataSetKey);
       
        String score = req.getParameter("score");
        
        String tried = req.getParameter("tried");
        
        String total = req.getParameter("total");
        
        String hasQuit = req.getParameter("hasQuit");
        
        String wrong = req.getParameter("wrong");
        
        String right = req.getParameter("right");
        
 //       private String convertmywrongarrayintoastring (wrong) {
 //       	String wrongstring = " ";    
 //      	for (int i = 0; i < wrong.length; i++) {
 //      		wrongstring = wrongstring + wrong[i];
 //     	}
 //    return wrongstring;
 //    }
        
        blsDataSet.setProperty("date", date);
        blsDataSet.setProperty("content", content);
        blsDataSet.setProperty("score", score);
        blsDataSet.setProperty("right", right);
        blsDataSet.setProperty("wrong", wrong);
        blsDataSet.setProperty("tried", tried);
        blsDataSet.setProperty("total", total);
        blsDataSet.setProperty("hasQuit", hasQuit);
        
        

        DatastoreService datastore =
                DatastoreServiceFactory.getDatastoreService();
        datastore.put(blsDataSet);

        resp.sendRedirect("http://www.google.com/search?q="+blsDataSetName);
    }

}

